package com.fingard.dsp.bank.directbank.allinpay03;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.allinpay03.util.CryptoUtil;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;

import java.util.HashMap;
import java.util.Map;

public class AllInPay03SinglePay extends AllInPay03Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1908);
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "");
            if (actItem == null) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req1908.singleDetail.payAct + "]商户配置信息";
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            Map<String, Object> bizContent = new HashMap<String, Object>();
            bizContentPut(bizContent, "transCode", "B1006_0004_92");
            bizContentPut(bizContent, "sourceCustId", actItem.merchantId);
            bizContentPut(bizContent, "targetCustId", p_req1908.singleDetail.recAct);
            bizContentPut(bizContent, "transferType", "1");
            bizContentPut(bizContent, "custName", p_req1908.singleDetail.recName);
            bizContentPut(bizContent, "mobile", p_req1908.singleDetail.cellPhone);
            bizContentPut(bizContent, "idNo", p_req1908.singleDetail.certNum);
            bizContentPut(bizContent, "transferAmt", StringHelper.amountMultiply100(p_req1908.singleDetail.payAmount));
            bizContentPut(bizContent, "frozenAmt", "0");
            bizContentPut(bizContent, "amtType", "1");
            bizContentPut(bizContent, "bizType", "6002");
            bizContentPut(bizContent, "orderNo", p_req1908.singleDetail.askNumber);
            bizContentPut(bizContent, "transferRule", "1");
            String remark = p_req1908.singleDetail.abstractATS + "+" + p_req1908.singleDetail.purpose;
            bizContentPut(bizContent, "remark", remark.length() > 64 ? remark.substring(0, 64) : remark);
            bizContentPut(bizContent, "senceType", "1");

            String source = getSourceString(bizContent);
            WriteBankLogLn("AllInPay03SinglePay source : " + source);
            String transSign = CryptoUtil.doSignBySHA1withRSA(source,actItem.ownCertFilePath);
            bizContentPut(bizContent, "transSign", transSign);
            String result = doSendData("appwallet", "appservice/accountTransfer", bizContent, actItem);
            if (result != null) {
                JSONObject resObj = JSONObject.parseObject(result);
                JSONObject data = resObj.getJSONObject("data");
                String subCode = resObj.getString("subCode");
                String subMsg = resObj.getString("subMsg");
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = subMsg;
                tmpRetDetail.transState = subCode;
                ;
                tmpRetDetail.payInfoCode = subCode;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.toString();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1944);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req1944.payAct, "");
            if (actItem != null) {
                Map<String, Object> bizContent = new HashMap<String, Object>();
                bizContentPut(bizContent, "transCode", "B9990_0003_02");
                bizContentPut(bizContent, "custId", p_req1944.recAct);
                bizContentPut(bizContent, "orderNo", p_req1944.askNumber);

                String result = doSendData("paycenter", "orderservice/orderQuery", bizContent, actItem);

                if (result != null) {
                    JSONObject resObj = JSONObject.parseObject(result);
                    String subCode = resObj.getString("subCode");
                    String subMsg = resObj.getString("subMsg");

                    JSONObject resObjs = JSONObject.parseObject(resObj.getString("data"));
                    if (resObjs != null) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = subMsg;
                        tmpRetDetail.transState = subCode;
                        tmpRetDetail.payInfoCode = subCode;
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = subMsg;
                        if ("5041".equals(subCode)) {
                            if (canBeNotExist(p_req1944, tmpRetDetail, 45)) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = subMsg;
                                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                                tmpRetDetail.payInfoCode = "E1302";
                            }
                        }
                    }
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + p_req1944.payAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.toString();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
